reading-notes

What Are Dunder Methods?

In Python, special methods are a set of predefined methods you can use to enrich your classes. They are easy to recognize because they start and end with double underscores,

def add_transaction(self, amount):
    if not isinstance(amount, int):
        raise ValueError('please use int for amount')
    self._transactions.append(amount)

Callable Python Objects: call

class Account:
    def __call__(self):
        print('Start amount: {}'.format(self.amount))
        print('Transactions: ')
        for transaction in self:
            print(transaction)
        print('\nBalance: {}'.format(self.balance))

Enriching a Simple Account Class

Throughout this article I will enrich a simple Python class with various dunder methods to unlock the following language features:

Initialization of new objects Object representation Enable iteration Operator overloading (comparison) Operator overloading (addition) Method invocation Context manager support (with statement)

What is probability?

  1. Flipping a heads
  2. Flipping a tails

Three Sigma Rule

Z-score